package com.sen.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.dao.EnterstockDao;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.EmpEntity;
import com.sen.entity.EnterstockEntity;
import com.sen.entity.EnterstockdetailEntity;
import com.sen.service.DruginfoService;
import com.sen.service.EmpService;
import com.sen.service.EnterstockService;
import com.sen.service.EnterstockdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service("enterstockService")
public class EnterstockServiceImpl extends ServiceImpl<EnterstockDao, EnterstockEntity> implements EnterstockService {

    @Autowired
    private EnterstockdetailService enterstockdetailService;
    @Autowired
    private EmpService empService;
    @Autowired
    private DruginfoService druginfoService;


    @Override
    public Page<EnterstockEntity> queryAll(Map<String,String> stockheadMap) {
        QueryWrapper<EnterstockEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("enterDate");
        //根据订单号查询
        if (!StringUtils.isEmpty(stockheadMap.get("hno"))){
            wrapper.eq("hno",stockheadMap.get("hno"));}
        if (!StringUtils.isEmpty(stockheadMap.get("name"))){
            //查询指定员工员工
            EmpEntity empEntity = empService.getOne(new QueryWrapper<EmpEntity>().eq("name", stockheadMap.get("name")));
            if(!StringUtils.isEmpty(empEntity)) {
                wrapper.eq("eid",empEntity.getEid());}
        }

        //获取分页对象
        Page<EnterstockEntity> page = new Page<>(Long.parseLong(stockheadMap.get("page")),Long.parseLong(stockheadMap.get("size")));
        Page<EnterstockEntity> enterstockEntityPage = this.baseMapper.selectPage(page, wrapper);
        //取出所有的值
        List<EnterstockEntity> enterstockEntities = enterstockEntityPage.getRecords();

        for (EnterstockEntity enterstockEntity : enterstockEntities) {
            //获得入库人姓名
            EmpEntity empEntity = empService.getById(enterstockEntity.getEid());
            enterstockEntity.setSname(empEntity.getName());
            //获取详情
            List<EnterstockdetailEntity> enterstockdetailEntities = enterstockdetailService.list(new QueryWrapper<EnterstockdetailEntity>().eq("esid", enterstockEntity.getEsid()));
            //遍历获取药品名
            for (EnterstockdetailEntity enterstockdetailEntity : enterstockdetailEntities) {
                DruginfoEntity druginfoEntity = druginfoService.getById(enterstockdetailEntity.getDid());
                enterstockdetailEntity.setDname(druginfoEntity.getMname());
            }
            //加入到详细中
            enterstockEntity.setEntityList(enterstockdetailEntities);

        }
        enterstockEntityPage.setRecords(enterstockEntities);

        return enterstockEntityPage;

    }

}
